package com.henu.engineer.controller.teacher

import com.alibaba.fastjson.JSONObject
import com.henu.engineer.StringUtils
import com.henu.engineer.dao.entity.teacher.CourseGroup
import com.henu.engineer.service.LoginService
import com.henu.engineer.service.teacher.CourseGroupService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * 查看课程组信息接口
 * @author noexcept
 * @since 2021/8/11 23:43
 */
@RestController
@RequestMapping("/tc")
class CourseGroupController {

    @Autowired
    lateinit var courseGroupService: CourseGroupService

    @Autowired
    lateinit var loginService: LoginService

    /**
     * 返回当前老师的所有课程组信息
     */
    @GetMapping("/allCourseGroup")
    fun allCourseGroup(): String? {
        val res = JSONObject()
        res["code"] = 1
        res["msg"] = "success"
        res["data"] = null

        if (loginService.currentTeacher == null) {
            res["code"] = 0
            res["msg"] = "教师未登录"
            return res.toJSONString()
        }

        val teacherId = loginService.currentTeacher!!.id

        if (StringUtils.isEmpty(teacherId)) {
            res["code"] = 0
            res["msg"] = "blank teacherId"
            return res.toJSONString()
        }
        res["data"] = courseGroupService.getCourseGroup(teacherId!!)
        return res.toJSONString()
    }

    /**
     * 返回某课程组的所有教师的名字和id
     */
    @GetMapping("/courseGroupMember")
    fun courseCourseGroupMember(courseGroupId: String?): String? {
        val res = JSONObject()
        if (StringUtils.isEmpty(courseGroupId)) {
            res["code"] = 0
            res["msg"] = "blank courseGroupId"
            return res.toJSONString()
        }
        res["code"] = 1
        res["msg"] = "success"
        res["data"] = courseGroupService.getCourseGroupMember(courseGroupId!!)
        return res.toJSONString()
    }
}